#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2025/2/5 16:19
# @Author  : Fancy
# @FileName: mysql.py
# @Software: PyCharm
# @runstate    ：
# clients/mysql.py
import pymysql
from db_compare.clients.base import BaseClient


class MySQLClient(BaseClient):
    """MySQL 客户端实现"""

    def __init__ (self, config):
        self.config = config
        #print(f"MySQLClient 初始化, config: {self.config.connect_args}")
    def get_conn(self):
        #print ("获取 MySQL 连接")
        #print (f"连接配置: {self.config.connect_args}")

            #connection = pymysql.connect (
        return pymysql.connect(
            host=self.config.connect_args["host"],
            user=self.config.connect_args["user"],
            password=self.config.connect_args["password"],
            database=self.config.connect_args["database"],
            port=int (self.config.connect_args["port"])
        )


    def fetch_stream(self, table: str, batch_size: int = 1000):
        """流式获取数据"""
        offset = 0
        conn = self.get_conn()
        try:
            while True:
                with conn.cursor() as cursor:
                    #print ("开始执行查询...")
                    cursor.execute (f"SELECT * FROM {table} LIMIT {batch_size} OFFSET {offset}")
                    results = cursor.fetchall ()
                    #print (f"查询结果: {results}")
                    return results
                    # 示例查询
                    # cursor.execute(f"SELECT * FROM {table} LIMIT {batch_size} OFFSET {offset}")
                    # batch = cursor.fetchall()
                    # if not batch:
                    #     break
                    # yield batch
                    # offset += batch_size
                    # print (f"获取表 {table} 的数据流，每批 {batch_size} 条")
        finally:
            conn.close()
